#include<stdio.h>
#include<fcntl.h>
#include<string.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"

char* create_vfs(char vfs_label[], int size)
{
	FILE *fp,*create_pointer;
	int a;
	int i;
	int fds;
	char ab[30];
	main_header main_header_var;
	file_descriptor fd,fd1,fd2;
	freelist fl,fl1,fl2;
	fl.a =0;
	int ij=0;
	int flag = 0;
	fds = size+(size*0.3);
	//printf("%d",fds);
	if(size<1 || size >100000)
		return ERR_VFS_CREATE_05;
	
	
	while(vfs_label[ij]!='\0')
	{
		if(vfs_label[ij]=='/')
			flag = 1;
			ij++;
	}
	if(flag == 1)
		return ERR_VFS_CREATE_04;
	
	create_pointer = fopen( vfs_label, "ab");
	if( create_pointer == NULL)	
			return ERR_VFS_CREATE_01;
			
	strcpy(main_header_var.label,vfs_label);
	main_header_var.no_used_file_descriptors = 1;
	main_header_var.max_no_file_descriptors =fds;
	main_header_var.no_used_blocks=0;
	
	
	
	
	strcpy(ab,vfs_label);
	strcat(ab,".dat");
	fp = fopen(ab, "wb");
	
	int x=ftell(fp);
	
	fwrite(&main_header_var,sizeof(main_header),1,fp);
	x = ftell(fp);
	
	strcpy(fd1.file_name,"home");
	strcpy(fd1.location_path,"/home");
	
	fwrite(&fd1,sizeof(file_descriptor),1,fp);

	
	for(i=1;i<fds;i++)
	{
		fwrite(&fd,sizeof(file_descriptor),1,fp);	
	}
	x = ftell(fp);

	int l;
	fl1.a = 1;
	fwrite(&fl1,sizeof(freelist),1,fp);
	
	for(l=1;l<fds;l++)
	{
			fwrite(&fl,sizeof(freelist),1,fp);
	}
	x = ftell(fp);

	main_header_var.headersize=x;
	rewind(fp);
	fwrite(&main_header_var,sizeof(main_header),1,fp);
	fseek(fp,x,SEEK_SET);
	for(i=0;i<fds;i++)
	{
		fwrite("0",1,1024,fp);
	}
	
	x = ftell(fp);
	
fclose(fp);
return "createvfs_SUCCESS";

//free(fl);
//free(fd);
}
	
